package com.talkweb.osmharmony;

import ohos.location.Location;
import ohos.location.Locator;
import ohos.location.LocatorCallback;
import ohos.location.RequestParam;

public class LocationListenerProxy implements LocatorCallback {

    private final Locator mLocator;
    private LocatorCallback mListener = null;

    public LocationListenerProxy(final Locator pLocator) {
        mLocator = pLocator;
    }

    public boolean startListening(final LocatorCallback pListener, final int pUpdateTime, final int pUpdateDistance) {
        boolean result = false;
        mListener = pListener;

        if(mLocator.isLocationSwitchOn()){
            result = true;
            RequestParam requestParam = new RequestParam(RequestParam.PRIORITY_FAST_FIRST_FIX, pUpdateTime, pUpdateDistance);
            mLocator.startLocating(requestParam, this);
        }

        return result;
    }

    public void stopListening() {
        mListener = null;
        mLocator.stopLocating(this);
    }

    @Override
    public void onLocationReport(Location location) {
        if (mListener != null) {
            mListener.onLocationReport(location);
        }
    }

    @Override
    public void onStatusChanged(int type) {
        if (mListener != null) {
            mListener.onStatusChanged(type);
        }
    }

    @Override
    public void onErrorReport(int type) {
        if (mListener != null) {
            mListener.onErrorReport(type);
        }
    }
}
